using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entities;

namespace DAL
{
    public class tbNhanvienDAL
    {
              private SqlConnection cn = null;
        public tbNhanvienDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(tbNhanvien nv)
        {
            SqlCommand cm = new SqlCommand("Nhan_vien_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@tennv", nv.Tennv);
            cm.Parameters.AddWithValue("@gioitinh", nv.Gioitinh);
            cm.Parameters.AddWithValue("@tendangnhap", nv.Tendangnhap);
            cm.Parameters.AddWithValue("@matkhau", nv.Matkhau);
            cm.Parameters.AddWithValue("@email", nv.Email);
            cm.Parameters.AddWithValue("@sodienthoai", nv.Sodthoai);
            cm.ExecuteNonQuery();
        }
       public void Update(tbNhanvien nv)
       {
           SqlCommand cm = new SqlCommand("Nhan_vien_Update", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@manv", nv.Manv);
           cm.Parameters.AddWithValue("@tennv", nv.Tennv);
           cm.Parameters.AddWithValue("@gioitinh", nv.Gioitinh);
           cm.Parameters.AddWithValue("@tendangnhap", nv.Tendangnhap);
           cm.Parameters.AddWithValue("@matkhau", nv.Matkhau);
           cm.Parameters.AddWithValue("@email", nv.Email);
           cm.Parameters.AddWithValue("@sodienthoai", nv.Sodthoai);
           cm.ExecuteNonQuery();
       }
       public void Delete(int iD)
       {
           SqlCommand cm = new SqlCommand("Nhan_vien_Delete", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@manv", iD);
           cm.ExecuteNonQuery();
       }
       public tbNhanvienCollection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
       {
           SqlCommand cm = new SqlCommand("Nhan_vien_Get", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@Where", where);
           cm.Parameters.AddWithValue("@PageIndex", pageIndex);
           cm.Parameters.AddWithValue("@PageSize", pageSize);
           cm.Parameters.AddWithValue("@OrderBy", orderBy);
           cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
           cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

           SqlDataReader dr = cm.ExecuteReader();
           tbNhanvienCollection tbnhanvienCollection = new tbNhanvienCollection();

           while (dr.Read())
           {
               tbNhanvien tbnhanvien = new tbNhanvien();
               tbnhanvien.Manv = int.Parse(dr.GetValue(0).ToString());
               tbnhanvien.Tennv = dr.GetValue(1).ToString();
               tbnhanvien.Gioitinh =dr.GetValue(2).ToString();
               tbnhanvien.Tendangnhap = dr.GetValue(3).ToString();
               tbnhanvien.Matkhau = dr.GetValue(4).ToString();
               tbnhanvien.Email  = dr.GetValue(5).ToString();
               tbnhanvien.Sodthoai = dr.GetValue(6).ToString();
               tbnhanvienCollection.Add(tbnhanvien);
           }
           dr.Close();
           totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
           return tbnhanvienCollection;
       }
    }
}
